//
//  YMNewfeatureViewController.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/22.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

let NewfeatureViewCellID = "cellID"

class YMNewfeatureViewController: UIViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    func setupUI() {
        view.addSubview(collectionView)
        collectionView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
    }
    
    //懒加载控件
    lazy var collectionView: UICollectionView = {
        
       let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .Horizontal
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        view.registerClass(YMCollectionViewCell.self, forCellWithReuseIdentifier: NewfeatureViewCellID)
        view.backgroundColor = RandomColor()
        
        view.pagingEnabled = true
        view.dataSource = self
        view.delegate = self
        view.bounces = false
        view.showsVerticalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        
        return view
        
    }()
    
}

extension YMNewfeatureViewController:UICollectionViewDataSource ,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewfeatureViewCellID, forIndexPath: indexPath) as! YMCollectionViewCell
//        cell.backgroundColor = RandomColor()
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int(scrollView.contentOffset.x/SCREENW)
        
        if index == 3 {
            let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! YMCollectionViewCell
            
            cell.startAnim()
        }
        
    }
    
    
    
}


class YMCollectionViewCell :UICollectionViewCell{
    
    var imageIndex:Int = 0{
        
        didSet{
     bgImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //开始动画
    func startAnim() {
        
        startButton.hidden = false
        
        
        self.startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        self.startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) in
            
            self.startButton.userInteractionEnabled = true
        }
        
        
    }
    
    
    func setupUI() {
        backgroundColor = RandomColor()
        
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
        startButton.hidden = true
        
    }
    
    func buttonClick() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwiftRootViewControllerNoti, object: "newFeature")
        
    }
    
    //懒加载控件
    
    lazy var bgImageView: UIImageView = UIImageView()
    lazy var startButton:UIButton = {
       
        let bt = UIButton()
        bt.addTarget(self, action: #selector(YMCollectionViewCell.buttonClick), forControlEvents: .TouchUpInside)
        bt.setBackgroundImage(UIImage (named: "new_feature_finish_button"), forState: .Normal)
        bt.setTitle("欢迎光临", forState: .Normal)
        bt.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        bt.titleLabel?.font = UIFont.systemFontOfSize(18)
        bt.sizeToFit()
        
        return bt
    }()
    
    
    
}


